import { state } from "../../config.js";

interface PoInfo {
  x: number;
  y: number;
  h: number;
  w: number;
}

export const collide = (bullel: PoInfo, role: PoInfo, cb: Function) => {
  let rp = getPoint(role);
  let bp = getPoint(bullel);
  let sumX = Math.abs(rp.x1 + rp.x2 - bp.x1 - bp.x2);
  let x = Math.abs(rp.x1 - rp.x2) + Math.abs(bp.x1 - bp.x2);
  let sumY = Math.abs(rp.y1 + rp.y2 - bp.y1 - bp.y2);
  let y = Math.abs(rp.y1 - rp.y2) + Math.abs(bp.y1 - bp.y2);
  if (sumX <= x - state.roleSpeed / 2 && sumY < y) {
    cb();
  }
};

export const getPoint = (p: { x: number; y: number; w: number; h: number }) => {
  return {
    x1: p.x,
    x2: p.x + p.w,
    y1: p.y,
    y2: p.y + p.h,
  };
};
